﻿using System;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace Sharp.Infrastructure.Crosscutting
{
    /// <summary>
    /// 日志帮助类
    /// </summary>
    /// <remarks>日志使用注意事项：记录跟踪信息如果数据量很大，则优先级设置为最低。以便在生产环境下设置日志过滤级别。</remarks>
    public class LogHelper
    {
        static LogHelper()
        {
            IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
            var logWriterFactory = new LogWriterFactory(configurationSource);
            Logger.SetLogWriter(logWriterFactory.Create());
        }
        public delegate void LogRecordHandler(object sender, EventArgs e);

        public static event LogRecordHandler OnLogRecorded;

        /// <summary>
        /// 错误或应用程序崩溃。
        /// </summary>
        /// <param name="message"></param>
        public static void Critical(object message)
        {
            Write(message, Category.General, Priority.Normal, TraceEventType.Critical);
        }
        /// <summary>
        /// 可恢复的错误
        /// </summary>
        /// <param name="message"></param>
        public static void Error(object message)
        {
            Write(message, Category.General, Priority.Normal, TraceEventType.Error);
        }
        /// <summary>
        /// 非关键性问题
        /// </summary>
        /// <param name="message"></param>
        public static void Warning(object message)
        {
            Write(message, Category.General, Priority.Normal, TraceEventType.Warning);
        }
        /// <summary>
        /// 跟踪调试
        /// </summary>
        /// <param name="message"></param>
        public static void Verbose(object message)
        {
            Write(message, Category.General, Priority.Normal, TraceEventType.Verbose);
        }
        /// <summary>
        /// 信息性消息
        /// </summary>
        /// <param name="message"></param>
        public static void Information(object message)
        {
            Write(message, Category.General, Priority.Normal, TraceEventType.Information);
        }
        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="message"></param>
        /// <param name="category"></param>
        /// <param name="priority"></param>
        /// <param name="traceEventType"></param>
        public static void Write(object message, string category, int priority, TraceEventType traceEventType)
        {
            Logger.Write(message, category, priority, 2050, traceEventType);

            if (OnLogRecorded != null)
            {
                OnLogRecorded.Invoke(message, null);

            }
        }
        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="categories"></param>
        /// <param name="logMessage"></param>
        public static void Log(string[] categories, string logMessage)
        {
            foreach (var v in categories)
            {
                Logger.Write(logMessage, v, Priority.Normal, 2050, TraceEventType.Information);
            }
        }
    }
    /// <summary>
    /// 分类
    /// </summary>
    public struct Category
    {
        public const string General = "普通";
        public const string Trace = "跟踪";
    }
    /// <summary>
    /// 优先级
    /// </summary>
    public struct Priority
    {
        public const int Lowest = 0;
        public const int Low = 1;
        public const int Normal = 2;
        public const int High = 3;
        public const int Highest = 4;
    }
}